// 请求器实例，挂载于window._request
import axios from 'axios'
import qs from 'qs'
import {prodApi, devApi} from '../../my-config'
import {login} from '../utils/common'

var config = {
  timeout: 120000,
  headers: {
    // 'Enable-App-Encrypt': '1',
    'Content-Type': 'application/json'
  },
  // transformRequest: qs.stringify
}

var axiosInstance = axios.create({
  baseURL: process.env.NODE_ENV === 'production' ? prodApi : devApi,
  ...config
})

axiosInstance.interceptors.request.use(requestDataHandler);
axiosInstance.interceptors.response.use(responseDataHandler);

// 拦截器的写法请参考 @see https://www.kancloud.cn/yunye/axios/234845#_395

// 请求拦截器
function requestDataHandler(req) {
  // req.headers['Authorization'] = window.localStorage.getItem('token') || ''
  return req
}

// 响应拦截器
async function responseDataHandler(res) {
  //用户信息错误
  // if (res.data.code == 2002001 || res.data.code == 2002002 || res.data.code == 2002004 ||res.data.code ==2001010) {
  //   localStorage.removeItem('secret');
  //   localStorage.removeItem('userInfo');
  //   localStorage.removeItem('token');
  //   this.$message('用户信息错误');
  //   await login();
  //   return false
  // }
  return res.data;
}

export default axiosInstance
